﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace CjcPhotoPrint
{
	public class Straighten : Transformation
	{
		private double angle = 0;

		public Straighten() : base( "Straighten" )
		{
		}

		public double Angle
		{
			get { return angle; }
			set { angle = value; OnPropertyChanged( "Drawing" ); }
		}

		public override Drawing GetDrawing( Drawing parent )
		{
			var size = RelativeSize( parent.Bounds.Width / parent.Bounds.Height/* aspectRatio*/ );
			var rect = new Rect( new Point(), size );

			var rotateTransform = new RotateTransform( Angle, size.Width / 2, size.Height / 2 );
			var newRect = rotateTransform.TransformBounds( rect );

			var scale = Math.Max( newRect.Height / rect.Height, newRect.Width / rect.Width );
			var scaleTransform = new ScaleTransform( scale, scale, size.Width / 2, size.Height / 2 );

			var transform = new TransformGroup();
			transform.Children.Add( rotateTransform );
			transform.Children.Add( scaleTransform );

			var straightenGroup = new DrawingGroup { Transform = transform };
			straightenGroup.Children.Add( parent );

			var clipGroup = new DrawingGroup { ClipGeometry = new RectangleGeometry( new Rect( size ) ) };
			clipGroup.Children.Add( straightenGroup );

			return clipGroup;
		}
	}
}